package edu.iss.demo.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import edu.iss.demo.model.LeaveType;
import edu.iss.demo.repository.LeaveTypeRepository;

@Service
public class LeaveTypeServiceImpl implements LeaveTypeService {
	
	@Resource
	private LeaveTypeRepository leaveTypeRepository;

	@Override
	@Transactional
	public LeaveType create(LeaveType l) {
		LeaveType createdLeaveType = l;
		return leaveTypeRepository.save(createdLeaveType);
	}
	
	@Override
	@Transactional
	public LeaveType findByCode(String code) {
		return leaveTypeRepository.findOne(code);
	}

	@Override
	@Transactional
	public LeaveType delete(String code){
		LeaveType deletedLeaveType = leaveTypeRepository.findOne(code);
		
		leaveTypeRepository.delete(deletedLeaveType);
		return deletedLeaveType;
	}

	@Override
	@Transactional
	public List<LeaveType> findAll() {
		return leaveTypeRepository.findAll();
	}

	@Override
	@Transactional
	public LeaveType update(LeaveType l){
		LeaveType updatedLeaveType = leaveTypeRepository.findOne(l.getLeaveCode());
		
		if (updatedLeaveType== null);
		
		updatedLeaveType.setLeaveCode(l.getLeaveCode());
		updatedLeaveType.setLeaveType(l.getLeaveType());
		updatedLeaveType.setDescription(l.getDescription());
		updatedLeaveType.setNumOfDay(l.getNumOfDay());
		leaveTypeRepository.flush();
		return updatedLeaveType;
	}

}
